package net.adetech.crj.utils;

import com.jfinal.kit.PropKit;
import net.adetech.crj.utils.entity.ServiceRequest;
import net.adetech.crj.utils.entity.ServiceResponse;
import org.apache.commons.net.ftp.FTPClient;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

public class ServiceUtil {



    //static String tempPath = "/Users/zhangsong/Downloads/tempRequest/";

    static String ftpIp = PropKit.get("ftp_out2in_ip");
    static int ftpPort = PropKit.getInt("ftp_out2in_port");
    static String path = PropKit.get("ftp_out2in_path");
    static String ftpUsername = PropKit.get("ftp_out2in_username");
    static String ftpPassword = PropKit.get("ftp_out2in_password");
    static String ftpLocalPath = PropKit.get("ftp_out2in_localpath");

    private static Map<String,CountDownLatch> countDownLatchMap = new HashMap<String, CountDownLatch>();
    private static Map<String,ServiceResponse> serviceResponseMap = new HashMap<String, ServiceResponse>();

    public static void setResponse(String uuid,ServiceResponse response)
    {
        serviceResponseMap.put(uuid,response);
        countDownLatchMap.get(uuid).countDown();
        countDownLatchMap.remove(uuid);
    }

    public static ServiceResponse call(String serviceName, Map<String,Object> params)
    {
        String uuid = UUID.randomUUID().toString();
        ServiceRequest request = new ServiceRequest();
        request.setServiceId(uuid);
        request.setServiceName(serviceName);
        request.setParams(params);
        String requestString =  SerializeUtil.objectToString(request);

        try {
            uploadRequest(uuid,requestString);

            CountDownLatch latch = new CountDownLatch(1);
            countDownLatchMap.put(uuid,latch);
            latch.await();
            ServiceResponse response = serviceResponseMap.get(uuid);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean callAsync(String serviceName, Map<String,Object> params)
    {
        String uuid = UUID.randomUUID().toString();
        ServiceRequest request = new ServiceRequest();
        request.setServiceId(uuid);
        request.setServiceName(serviceName);
        request.setParams(params);
        String requestString =  SerializeUtil.objectToString(request);

        try {
            uploadRequest(uuid,requestString);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    private static void uploadRequest(String uuid,String requestString) throws Exception
    {
        String tempFileName = ftpLocalPath+"request_"+uuid+".xml";
        Element root = DocumentHelper.createElement("request");
        root.addText(requestString);
        Document document = DocumentHelper.createDocument(root);
        OutputFormat format = new OutputFormat("\t",true);
        format.setEncoding("GBK");
        XMLWriter writer = null;
        writer = new XMLWriter(new FileOutputStream(tempFileName),format);
        writer.write(document);
        writer.close();
        FTPClient ftp = new FTPClient();
        ftp.connect(ftpIp,ftpPort);
        ftp.login(ftpUsername,ftpPassword);
        ftp.changeWorkingDirectory(path);
        File localFile = new File(tempFileName);
        InputStream is = new FileInputStream(localFile);
        ftp.storeFile("request_"+uuid+".xml",is);
        is.close();
        localFile.delete();
    }
}

